<template>
  <div class="table-page">
    <div id="slot-header" v-if="showHeader">
      <transition name="draw">
        <div v-show="expandHeader">
          <slot name="header"></slot>
        </div>
      </transition>
      <div v-if="showCollapse" class="expand-header" @click="toggle">
        <span class="mr-5">{{expandHeader ? collapseText : expandText}}</span>
        <i :class="[expandHeader ? 'icon el-icon-caret-bottom rotate' : 'icon el-icon-caret-bottom']"></i>
      </div>
    </div>
    <slot name="table" :tableHeight="tableHeight"></slot>
    <el-pagination
      id="pagination"
      v-if="showPager"
      background
      :layout="isMobile ? 'pager' : 'total, sizes, prev, pager, next, jumper'"
      :total="total"
      :current-page="current"
      :page-size.sync="pageSize"
      :hide-on-single-page="true"
      @size-change="sizeChange"
      @current-change="currentChange"
      @prev-click="currentChange"
      @next-click="currentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'TablePage',
  props: {
    // 是否显示slot=header
    showHeader: {
      type: Boolean,
      default: false
    },
    // 是否显示分页器
    showPager: {
      type: Boolean,
      default: true
    },
    // 是否显示展开、收起按钮
    showCollapse: {
      type: Boolean,
      default: false
    },
    // 收起文案
    collapseText: {
      type: String,
      default: '收起筛选项'
    },
    // 展开文案
    expandText: {
      type: String,
      default: '展开筛选项'
    },
    // 分页器当前页
    current: {
      type: Number,
      default: 1
    },
    // 分页器每页条数
    pageSize: {
      type: Number,
      default: 20
    },
    // 分页器total
    total: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      tableHeight: 100,
      expandHeader: true
    }
  },
  computed: {
    ...mapGetters([ 'isMobile' ])
  },
  watch: {
    total () {
      this.getTableHeight()
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.getTableHeight()
    })
    let _this = this
    window.onresize = () => {
      requestAnimationFrame(() => _this.getTableHeight())
    }
  },
  beforeDestroy (){
    window.onresize = null
  },
  methods: {
    getTableHeight () {
      let windowHeight = document.documentElement.clientHeight || document.body.clientHeight
      let slotHeaderHeight = 0
      if (this.showHeader) {
        slotHeaderHeight = document.getElementById('slot-header').getBoundingClientRect().height
      }
      // layout-header高40px;
      // layout-footer高40px;
      // 容器padding:10px 3px, 上下共20px;
      // 分页器高度40px
      // slot:header动态计算高度
      // PC端单页：40 + 40 + 20 = 100
      // PC端非单页：40 + 40 + 20 + 40 = 140
      // m端隐藏了footer，单页60，非单页100
      if (this.total <= this.pageSize) { // 单页时不用减去分页器所占高度40
        this.tableHeight = this.isMobile ? (windowHeight - 60 - slotHeaderHeight) : (windowHeight - 100 - slotHeaderHeight)
      } else {
        this.tableHeight = this.isMobile ? (windowHeight - 100 - slotHeaderHeight) : (windowHeight - 140 - slotHeaderHeight)
      }
    },
    sizeChange (size) {
      this.$emit('pageChange', this.current, size)
    },
    currentChange (current) {
      this.$emit('pageChange', current, this.pageSize)
    },
    toggle () {
      this.expandHeader = !this.expandHeader
      this.$nextTick(() => {
        this.getTableHeight()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.table-page {
  padding: 10px 3px;
}

#slot-header {
  padding: 0 20px 10px;
  .expand-header {
    margin-top: 3px;
    height: 20px;
    line-height: 20px;
    text-align: center;
    font-size: 12px;
    color: #606266;
    cursor: pointer;
    .icon {
      transition: all 200ms;
    }
    .rotate {
      transform: rotate(180deg);
    }
  }
}

#pagination {
  height: 40px;
  box-sizing: border-box;
  padding-top: 10px;
  float: right;
}

.draw-enter-active, .draw-leave-active {
  transition: all .2s;
}
.draw-enter, .draw-leave-active {
  transform: translate3d(0, -2rem, 0);
  opacity: 0;
}
.draw-leave-active {
  height: 0;
}
</style>
